/*
 * Copyright 2010 Florian Sauter
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.rentoudu.chat.server.servlet;

import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.utils.SystemProperty;
import com.google.appengine.api.utils.SystemProperty.Environment.Value;
import com.google.inject.Inject;

/**
 * After calling this servlet, the user will be logged out.
 * 
 * @author Florian Sauter
 */
public class LogoutServlet extends HttpServlet {
	
	private static final long serialVersionUID = -322503230537069243L;

	private final UserService userService;
	
	@Inject
	public LogoutServlet(final UserService userService) {
		this.userService = userService;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		Value systemEnvironment = SystemProperty.environment.value();
		
		String targetUrl = "";
		
		//TODO: add dynamic url according request
		if (SystemProperty.Environment.Value.Production.equals(systemEnvironment)) {
			targetUrl = "https://phone-messenger.appspot.com";
		} else if (SystemProperty.Environment.Value.Development.equals(systemEnvironment)) {
			targetUrl = "http://localhost:8888/";
		} else {
			targetUrl = "http://localhost:8888/";
		}
		
		String loginUrl = userService.createLoginURL(targetUrl);
		String logoutUrl = userService.createLogoutURL(loginUrl);
		
		resp.sendRedirect(logoutUrl);
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doGet(req, resp);
	}
}
